package infoPackage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;

public class LoadInfo{
	int nRivers = 0;
	int nSections = 0;
	ArrayList<River> rivers = null;
	ArrayList<Section> sections = null;
	
	public LoadInfo(){
		rivers = new ArrayList<River>();
		sections = new ArrayList<Section>();
	}
	
	public void parseInfo(FileInputStream file){
		//rivers = new ArrayList<River>();
		
	}
	
	public void xmlParser(File file){
		  try {
			  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			  DocumentBuilder db = dbf.newDocumentBuilder();
			  Document doc = db.parse(file);
			  doc.getDocumentElement().normalize();
			  
			  //Number of Rivers: <Rivers nRivers = "1" >
			  nRivers = Integer.parseInt(doc.getDocumentElement().getAttribute("nRivers"));
			  NodeList riversNode = doc.getElementsByTagName("River");
			  for (int river=0; river<nRivers; river++) {
				  Node firstNode = riversNode.item(river);
				  if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
					  Element firstRiver = (Element) firstNode;
					  String riverName = firstRiver.getAttribute("name");
					  int sections = Integer.parseInt(firstRiver.getAttribute("nSections"));
					  this.nSections += sections;
					  rivers.add(new River(riverName, river));
					  NodeList sectionsNode = firstRiver.getElementsByTagName("Section");   
					  for(int j=0; j<sections; j++){
							  Element firstSection = (Element) sectionsNode.item(j);
							  float offset = Float.parseFloat(firstSection.getAttribute("offset"));
							  float flow = Float.parseFloat(firstSection.getAttribute("flow"));
							  int narrow = Integer.parseInt(firstSection.getAttribute("narrow"));
							  float h = Float.parseFloat(firstSection.getAttribute("h"));
							  int access = Integer.parseInt(firstSection.getAttribute("access"));
							  boolean faune = Boolean.parseBoolean(firstSection.getAttribute("fauna"));
							  boolean flora = Boolean.parseBoolean(firstSection.getAttribute("flora"));
							  float people = Float.parseFloat(firstSection.getAttribute("people"));
							  boolean struct = Boolean.parseBoolean(firstSection.getAttribute("struct"));
							  boolean power = Boolean.parseBoolean(firstSection.getAttribute("power"));
							  float cost = Float.parseFloat(firstSection.getAttribute("cost"));
							  float size = Float.parseFloat(firstSection.getAttribute("size"));
							  float minVolume = Float.parseFloat(firstSection.getAttribute("minVolume"));
							  this.sections.add(new Section(river, offset, flow, narrow, h, access, faune, flora, people, struct, power, cost, size, minVolume));
					  }
				  }
			  }
			  
// DEBUG
//			  System.out.println("* Information of all rivers * ");
//			  System.out.println("* " + nRivers + " rivers and " + nSections + " sections * ");
//			  for(int z =0; z<nRivers; z++)
//				  rivers.get(z).printRiver();
// DEBUG			  
			  
		} catch (FileNotFoundException e) {
			System.out.println("Configuration file not found. Program will exit.");
			System.exit(-1);
		}catch (Exception e) {
			System.out.println("Bad configuration file. Program will exit.");
			System.exit(-1);
		}
	}
	
}
